//本地数据

var app = getApp();

Page({

  data: {
    imgurl: app.globalData.imgurl,
  },
  onLoad: function (e) {
    this.setData({
      goodtype:e.goodtype
    })
  },
  onShow() {
    var that = this;
    var totalPrice = '0.00';
    //购物车列表
    var carts = this.data.carts;
    wx.getStorage({
      key: 'ids',
      success: function (res) {
        console.log(res)
        var ids = res.data
        //  获取购物车列表
        that.getlist({
          ids: ids
        })
        that.setData({
          ids: ids
        })
      },
    })
    this.getAddr()
  },
  getAddr() {
    var that = this
    wx.request({
      url: app.globalData.url + 'addr_list_type?type=1&openid=' + wx.getStorageSync('openId'),
      success: function (res) {
        var addr = res.data
        if (!addr.id) {
          wx.showModal({
            title: '提示',
            content: '您还没有添加收货地址，点击确定去添加',
            showCancel: true,
            success: function (res) {
              console.log(res)
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/address/list/list?addrtype=1',
                })
              }
            },
          })
          return
        }
        that.setData({
          addr: res.data
        })
      }
    })
  },
  // 切换收货地址
  switchAddr() {
    wx.navigateTo({
      url: '/pages/address/list/list?addrtype=1',
    })
  },
  //购物车列表
  getlist: function (e) {
    var that = this;
    var ids = e.ids;
    var openId = wx.getStorageSync('openId')
    if (e.ids) {
      var data = {
        shopping_id: ids,
      }
    } else {
      var data = {
        shoppingid: ids
      }
    }

    wx.request({
      url: app.globalData.url + 'order_shopping?type='+this.data.goodtype,
      data: data,
      success: function (res) {
        if (typeof res.data[0] == 'object') {
          var list = res.data;
          for (var i = 0; i < list.length; i++) {
            list[i].priceSet = that.getPriceSet(list[i])
          }
          that.setData({
            list: list,
          })
        }
      }
    })
  },
  // 每个商品的租金价格
  getPriceSet: function (item) {
    return {
      heji: (item.product[0].zi[0].cuxiao_price * item.number).toFixed(2),
    }
  },
  summit: function (e) {
    var that = this
    var data = {
      openid: wx.getStorageSync('openId'),
      pid: that.data.ids,
      addr_id: that.data.addr.id,
      shopping_id: that.data.ids,
      content: '',
      pzid:that.data.pzid,
      type: this.data.goodtype,
    }
    wx.request({
      url: app.globalData.url + 'order_add_type',
      data: data,
      success: function (res) {
        var orderid = res.data.toString()
        console.log(orderid)
        wx.request({
          url: app.globalData.payurl + 'openid=' + wx.getStorageSync('openId') + '&order_id=' + res.data[0],
          success: function (res) {
            var payData = res.data
            if (payData.timeStamp) {
              wx.requestPayment({
                timeStamp: payData.timeStamp,
                nonceStr: payData.nonceStr,
                package: payData.package,
                signType: payData.signType,
                paySign: payData.paySign,
                success: function (res) {
                  console.log(res)
                  wx.redirectTo({
                    url: '/pages/index/success/success?success=1&atype=2',
                  })
                  // 给商家发送短信
                  that.sendMsg(orderid)
                },
                fail: function (res) {
                  console.log('支付失败')
                  wx.redirectTo({
                    url: '/pages/index/success/success?success=0&atype=2',
                  })
                },
                complete: function (res) {

                },
              })
            }
          },
          fail: res => {
            console.log('错误')
          }
        })
      }
    })
  }, // 给商家发送短信
  sendMsg(orderid) {
    wx.request({
      url: app.globalData.telurl + 'sendss',
      data: {
        order_id: orderid
      }
    })
  }
})